class Solution {
public:
    vector<int> mostCompetitive(vector<int>& nums, int k) {
        vector<int>ans;
        int n = nums.size();
        for (int i = 0; i < n; i++) {
            int x = nums[i];
            while (!ans.empty() && n - i + ans.size() > k && ans.back() > x) {
                ans.pop_back();
            }
            if (ans.size() < k)ans.push_back(x);
        }
        return ans;
    }
};